/**
 * Created by Administrator on 2017/7/19.
 */
define(function(require, exports){
	var $ = require('jquery');
	var util = require('util');
	var pubjs = require('pubjs');
	// var view  = require('@base/view');
	var popwin = require('@base/dialog').base;

	// 切换货币弹层
	var Base = popwin.extend({
		init: function( config ) {
			config = pubjs.conf( config, {
				'class': 'P-switch',
				"size": 's',
				"buttonConfig":{
					"ok": {
						"value":LANG("提交")
					}
				},
				'data':null,
				'title': LANG('选择展示货币'),
				'vModel': {
					'now': {}, // 当前货币
					'data': [],
					'select': 1
				}
			});

			this.Super('init', arguments);
		},
		afterBuild: function(){
			var self = this;
			self.Super('afterBuild',arguments);
			var el = self.getContainer('body');

			el.append($([
				'<div class="mt10" >',
				'<div class="mb10">'+LANG('此处货币更改仅为方便查看，实际账户消耗及余额仍为默认货币。')+'</div>',

				'<div class="M-formConGroup M-sidebarRadioCon">',
					'<div class="group-content">',
						'<div v-for="item in data" class="content" style="display:block">',
							'<div>',
								'<label class="" style="" v-bind:class="{\'act\': select == item.id}" v-show="item" >',
									'<input v-bind:value="item.id" type="radio" v-model="select" v-show="false">',
									'<i class="icon-circle-double icon-circle-thin mr5" v-show="true" v-bind:class="{\'icon-circle-thin\': select != item.id}"/>',
									'{{item.name}}' +' : '+'{{now.name}}' + ' = ' + '{{item.rate}}'+' : ' +'{{now.rate}}',
								'</label>',
							'</div>',
						'</div>',
					'</div>',
				'</div>',

				'</div>'
			].join('')));


			self.vm.mount();

			this.vm.watch('select', this.watchSelectChange);

		},
		watchSelectChange: function(value){

		},
		getData: function(){
			return this.vm.$.select;
		},
		onOk: function(){
			var id = this.getData();
			if(!id){
				pubjs.alert("请选择需要切换的货币类型");
				return false;
			}

			this.hide();
			// 点击切换货币
			if(id != pubjs.getCurrencyId()){

				pubjs.setCurrencyId(id);
				window.location.reload();
			}
			return false;
		},
		load: function(){
			var id = pubjs.getUserCurrent('Currency') || 1;
			pubjs.mc.send('category/getCurrencyRate', {'id': id}, this.onData.bind(this));
			return this;
		},
		onData: function(err, data){
			var self = this;
			var c = self.getConfig();

			if (err){
				pubjs.error(err.message);
				return false;
			}

			this.setData(data);
		},
		setData: function(data){
			var all = util.clone(data);
			var now = util.find(all, 1, 'rate');

			this.vm.set({
				'data': all,
				'select': pubjs.getCurrencyId(),
				'now': now
			});

			return this;
		},
		reset: function(){
			this.vm.reset();
			return this;
		}
	});

	exports.base = Base;
});